#pragma once

#include<iostream>
using namespace std;

namespace ZH
{
	template<class T>
	class shared_ptr
	{
	public:

		shared_ptr(T* ptr)
			:_ptr(ptr)
			,_pcount(new int(1))
		{}

		~shared_ptr()
		{
			if (--(*_pcount) == 0)
			{
				cout << "~shared_ptr()" << endl;
				delete _ptr;
				delete _pcount;
			}
		}

		shared_ptr(const shared_ptr& sp)
			:_ptr(sp._ptr)
			, _pcount(sp._pcount)
		{
			(*_pcount)++;
		}

		//spt2 = spt3;
		shared_ptr& operator=(const shared_ptr& sp)
		{
			if (sp._ptr != _ptr)
			{
				if (--(*_pcount) == 0)
				{
					cout << "~shared_ptr()" << endl;
					delete _ptr;
					delete _pcount;
				}
				_ptr = sp._ptr;
				_pcount = sp._pcount;
				(*sp._pcount)++;
			}
			return *this;
		}

	private:
		T* _ptr;
		T* _pcount;
	};

	void test1()
	{
		shared_ptr<int> spt1(new int(12));
		shared_ptr<int> spt2(spt1);

		shared_ptr<int> spt3(new int(123));

		spt2 = spt3;

	}

}